package com.novabluetooth.views;

import java.util.List;

import com.health.viewscontrols.NovaApplication;
import com.health.R;
import com.health.db.FDbManager;
import com.health.tablebean.SBCL_DATA;
import com.novabluetooth.adapter.ExamHistoryAdaptor;
import com.novabluetooth.services.BluetoothConstant;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 体检结果标注
 * 
 * @author nova
 *
 */
public class ExamHistoryActivity extends Activity implements OnItemClickListener {
	public static final int ResultXueya = 1;
	public static final int ResultTizhong = 2;
	public static final int ResultXuetang = 3;
	public static final int ResultXueyang = 4;
	public static final int ResultTiwen = 5;
	public static final int ResultXueHongDanbai = 6;
	public static final int ResultNiaoChangGui = 7;
	public static final int ResultMaiLv = 8;// 心率等同于脉率
	public static final int ResultECG = 9;
	public static final int ResultXueZhi = 10;
	public static final int ResultUpdata = 0x010;
	/**
	 * 百捷三合一
	 */
	public static final int ResultTRIPLE = 10;
	/**
	 * 胆固醇
	 */
	public static final int ResultDGC = 11;
	private static final String TAG = "ExamHistoryActivity";
	private ListView lv;
	private Button cancelBtn;
	static FDbManager fdbmanger = NovaApplication.fdbmanger;
	private ExamHistoryAdaptor adaptor;
	private String flag;
	private List<SBCL_DATA> gaoList;
	private List<SBCL_DATA> diya;
	private List<SBCL_DATA> mailv;
	private List<SBCL_DATA> dataList;
	List<SBCL_DATA> niaodanyuanList;// 尿胆原
	List<SBCL_DATA> qianxueList;// 潜血
	List<SBCL_DATA> danhongsuList;// 胆红素
	List<SBCL_DATA> tongtiList;// 酮体
	List<SBCL_DATA> putaotangList;// 葡萄糖
	List<SBCL_DATA> danbaizhiList;// 蛋白质
	List<SBCL_DATA> pHList;// ph
	List<SBCL_DATA> yaxiaoList;// 亚硝酸盐
	List<SBCL_DATA> baixibaoList;// 白细胞
	List<SBCL_DATA> bizhongList;// 比重
	List<SBCL_DATA> weishengsuList;// 维生素

	private static String IDCARD = "";
	java.text.DecimalFormat weightdf = new java.text.DecimalFormat("#.0");
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// List<SBCL_DATA> findAll = fdbmanger.findAllBT(SBCL_DATA.class);
		// Log.e("tag", findAll.size()+"tt");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.exam_history_list);
		flag = getIntent().getStringExtra("flag");
		Log.e(TAG, "onCreate:flag " + flag );

		lv = (ListView) findViewById(R.id.exam_history_lv);
		cancelBtn = (Button) findViewById(R.id.exam_history_cancel);

		if (NovaApplication.getUserInfo() != null) {
			IDCARD = NovaApplication.getUserInfo().getIDCARD();
			if (TextUtils.isEmpty(IDCARD)) {
				return;
			}
		}

		if (BluetoothConstant.XUEYA_TYPENAME.equals(flag)) {
			setSize(0.6, 0.5);
			gaoList = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '高压' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			diya = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '低压' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			mailv = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '脉率' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			adaptor = new ExamHistoryAdaptor(gaoList, diya, mailv, this, flag);
			String[] tvTexts = { "收缩压", "舒张压", "脉率", "检测时间" };
			setHead(tvTexts);
		} else if (BluetoothConstant.NIAOCHANGGUI_TYPENAME.equals(flag)) {
			setSize(0.9, 0.5);
			niaodanyuanList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"CL_TYPE = '尿胆原' AND IDCARD = '" + IDCARD + "'", "CL_TIME desc");
			qianxueList = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '潜血' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			danhongsuList = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '胆红素' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			tongtiList = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '酮体' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			putaotangList = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '葡萄糖' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			danbaizhiList = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '蛋白质' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			pHList = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = 'PH' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			yaxiaoList = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '亚硝酸盐' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			baixibaoList = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '白细胞' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			bizhongList = fdbmanger.findAllByWhere(SBCL_DATA.class, "CL_TYPE = '比重' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			weishengsuList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"CL_TYPE = '维生素' AND IDCARD = '" + IDCARD + "'", "CL_TIME desc");
			adaptor = new ExamHistoryAdaptor(flag, this, niaodanyuanList, qianxueList, danhongsuList, tongtiList,
					putaotangList, danbaizhiList, pHList, yaxiaoList, baixibaoList, bizhongList, weishengsuList);
//			String[] tvtext = { "尿胆原", "潜血", "胆红素", "酮体", "葡萄糖", "蛋白质", "PH", "亚硝酸盐", "白细胞", "比重", "维生素", "检测时间" };
			String[] tvtext = { "尿胆原", "尿潜血", "胆红素", "尿酮体", "尿糖", "尿蛋白", "PH", "亚硝酸盐", "白细胞", "比重", "维生素", "检测时间" };
			setHead(tvtext);
		}else if(BluetoothConstant.XUEZHI_TYPENAME.equals(flag)){
			setSize(0.8,0.5);
			dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"CL_TYPE = '" + flag + "' AND IDCARD = '" + IDCARD + "'", "CL_TIME desc");
			adaptor = new ExamHistoryAdaptor(dataList, this, flag);
			String[] tvtext = { flag, "检测时间" };
			setHead(tvtext);
		} else if (BluetoothConstant.ECG_TYPENAME.equals(flag)) {
			setSize(0.8, 0.5);
			dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"IDCARD = '" + IDCARD + "' AND  (CL_TYPE = '理邦心电诊断结果' or CL_TYPE = '心电诊断结果' )", "CL_TIME desc");
			adaptor = new ExamHistoryAdaptor(dataList, this, flag);
			String[] tvtext = { "心电诊断结果", "检测时间" };
			setHead(tvtext);
		}else if(BluetoothConstant.TIWEN_TYPENAME.equals(flag)){
			//体温
			setSize(0.6, 0.5);
			dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
				"IDCARD = '" + IDCARD + "' AND  (CL_TYPE = '" + flag +"' or CL_TYPE = '表面温度')" , "CL_TIME desc");
			Log.e(TAG, "IDCARD = '" + IDCARD + "' AND  (CL_TYPE = '" + flag +"' or CL_TYPE = '表面温度')");
			adaptor = new ExamHistoryAdaptor(dataList, this, flag);
			String[] tvtext = {"类型", "温度", "检测时间" };
			setHead(tvtext);
		}else if(BluetoothConstant.XUEYANG_TYPENAME.equals(flag)){
			setSize(0.6, 0.5);
			dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"IDCARD = '" + IDCARD + "' AND  (CL_TYPE = '" + flag +"' or CL_TYPE = '血氧')" , "CL_TIME desc");
			Log.e(TAG, "IDCARD = '" + IDCARD + "' AND  (CL_TYPE = '" + flag +"' or CL_TYPE = '血氧')");
			adaptor = new ExamHistoryAdaptor(dataList, this, flag);
//			String[] tvtext = {"类型", "血氧", "检测时间" };
			String[] tvtext = {"血氧", "检测时间" };
			setHead(tvtext);
		} else if(BluetoothConstant.TRIPLE_TYPENAME.equals(flag)){
			//百捷三合一
			setSize(0.6, 0.5);
			dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"IDCARD = '" + IDCARD + "' AND   DEVICE_TYPE = '"+BluetoothConstant.TRIPLE_TYPENAME+"'  " , "CL_TIME desc");
			Log.e(TAG, "IDCARD = '" + IDCARD + "' AND   DEVICE_TYPE ='"+BluetoothConstant.TRIPLE_TYPENAME+"' " );
			adaptor = new ExamHistoryAdaptor(dataList, this, flag);
			String[] tvtext = {"类型", "数据", "检测时间" };
			setHead(tvtext);
		} else if (BluetoothConstant.PW_01DEVICE_NAME.equals(flag)) {
			setSize(0.6, 0.5);
			gaoList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"CL_TYPE_NAME = '" + BluetoothConstant.PW_01DEVICE_NAME +"' AND IDCARD = '" + IDCARD + "'",
					"CL_TIME desc");
			adaptor = new ExamHistoryAdaptor(gaoList, this, flag);
			String[] tvTexts = { "总胆固醇", "甘油三酯", "高密","低密", "检测时间" };
			setHead(tvTexts);
		} else if (BluetoothConstant.TIZHONG_TYPENAME3.equals(flag)) {
			setSize(0.9, 0.5);
			// 体重 BMI 肌肉量  脂肪率  骨量  皮下脂肪  骨骼肌率  水分率  肌肉率  蛋白质 健康评估 肥胖等级 健康等级 测量时间
			List niaodanyuanList = fdbmanger.findAll(SBCL_DATA.class);
			dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"DEVICE_TYPE = '轻加脂肪秤' AND IDCARD = '" + IDCARD + "'", "CL_TIME desc");
			String[] tvtext = { "体重", "BMI", "肌肉量", "脂肪率", "骨量", "皮下脂肪", "水分率",
					"蛋白质", "健康评估","肥胖等级","健康等级", "检测时间" };
			adaptor = new ExamHistoryAdaptor(dataList, this, flag);
			setHead(tvtext);
		} else if ("FM2".equals(flag)) {
			setSize(0.8, 0.5);
			dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"DEVICE_TYPE = 'FM2' AND IDCARD = '" + IDCARD + "'", "CL_TIME desc");
			String[] tvtext = { "身高", "体重", "bmi", "检测时间" };
			adaptor = new ExamHistoryAdaptor(dataList, this, flag);
			setHead(tvtext);
		} else if ("RTCF_DH".equals(flag)) {
			setSize(0.9, 0.5);
			dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"DEVICE_TYPE = 'RTCF_DH' AND IDCARD = '" + IDCARD + "'", "CL_TIME desc");
			String[] tvtext = { "身高", "体重", "bmi", "体脂肪量", "体脂肪率", "内脏脂肪等级", "体水分率",
					"细胞内液", "细胞外液", "基础代谢", "得分",
					"检测时间" };
			adaptor = new ExamHistoryAdaptor(dataList, this, flag);
			setHead(tvtext);
		} else if ("PEF_SP".equals(flag)) {
			setSize(0.95, 0.5);
			dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"DEVICE_TYPE = '肺功能' AND IDCARD = '" + IDCARD + "'", "CL_TIME desc");
			String[] tvtext = { "fvc", "fev05", "fev1", "fev1_fvc", "pef", "pef25",
					"pef50", "pef75", "fev3", "peft", "evol",
					"检测时间" };
			adaptor = new ExamHistoryAdaptor(dataList, this, flag);
			setHead(tvtext);
		} else {
			// if ("体重".equals(flag))
			setSize(0.4, 0.5);
			dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
					"CL_TYPE = '" + flag + "' AND IDCARD = '" + IDCARD + "'", "CL_TIME desc");
			Log.e(TAG, "CL_TYPE = '" + flag + "' AND IDCARD = '" + IDCARD + "'");
			Log.e("tag", dataList.toString());
			Log.e("tag", flag + dataList.size());
			if ("心率".equals(flag)) {
				dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
						"CL_TYPE = '" + "脉率" + "' AND IDCARD = '" + IDCARD + "'", "CL_TIME desc");
				Log.e("脉率", dataList.toString());
			}
			if ("体重".equals(flag)){
				if(null!=dataList&&dataList.size()>0){
					JSONObject obj;
					for(SBCL_DATA s:dataList){
						if(s.getCL_VALUE().contains("lfWeightKg")){
							try {
								obj = new JSONObject(s.getCL_VALUE());
								Double weight = obj.getDouble("lfWeightKg");
								s.setCL_VALUE(weightdf.format(weight));
							} catch (JSONException e) {
								e.printStackTrace();
							}

						}
					}
				}
			}
			adaptor = new ExamHistoryAdaptor(dataList, this, flag);
			String[] tvtext = { flag, "检测时间" };
			setHead(tvtext);
		}
		lv.setAdapter(adaptor);
		lv.setOnItemClickListener(this);

		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ExamHistoryActivity.this.finish();
			}
		});

	}

	private void setSize(double width, double height) {
		WindowManager m = getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
		p.height = (int) (d.getHeight() * height); // 高度设置为屏幕的1.0
		p.alpha = 0.9f; // 设置本身透明度
		p.dimAmount = 0.0f; // 设置黑暗度
		p.width = (int) (d.getWidth() * width); // 宽度设置为屏幕的0.8
		getWindow().setAttributes(p);
	}

	private void setHead(String[] tvTexts) {

		TextView tv1 = (TextView) findViewById(R.id.exam_history_tv1);
		TextView tv2 = (TextView) findViewById(R.id.exam_history_tv2);
		TextView tv3 = (TextView) findViewById(R.id.exam_history_tv3);
		TextView tv4 = (TextView) findViewById(R.id.exam_history_tv4);
		TextView tv5 = (TextView) findViewById(R.id.exam_history_tv5);
		TextView tv6 = (TextView) findViewById(R.id.exam_history_tv6);
		TextView tv7 = (TextView) findViewById(R.id.exam_history_tv7);
		TextView tv8 = (TextView) findViewById(R.id.exam_history_tv8);
		TextView tv9 = (TextView) findViewById(R.id.exam_history_tv9);
		TextView tv10 = (TextView) findViewById(R.id.exam_history_tv10);
		TextView tv11 = (TextView) findViewById(R.id.exam_history_tv11);
		TextView timeTv = (TextView) findViewById(R.id.exam_history_time);
		TextView[] tvs = { tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8, tv9, tv10, tv11, timeTv };
		for (TextView textView : tvs) {
			if (textView.getVisibility() == View.VISIBLE) {
				textView.setVisibility(View.GONE);
			}
		}
		int length = tvTexts.length;
		for (int i = 0; i < length - 1; i++) {
			tvs[i].setVisibility(View.VISIBLE);
			tvs[i].setText(tvTexts[i]);
		}
		tvs[tvs.length - 1].setVisibility(View.VISIBLE);
		tvs[tvs.length - 1].setText(tvTexts[length - 1]);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent();
		if (BluetoothConstant.XUEYA_TYPENAME.equals(flag)) {
			intent.putExtra("高压", gaoList.get(position).getCL_VALUE().split("mmHg")[0]);
			intent.putExtra("低压", diya.get(position).getCL_VALUE().split("mmHg")[0]);
			intent.putExtra("脉率", mailv.get(position).getCL_VALUE().split("次")[0]);
			Log.e(TAG, gaoList.get(position).getCL_VALUE().split("mmHg")[0]);
			setResult(ResultXueya, intent);

		} else if (BluetoothConstant.TIZHONG_TYPENAME.equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE().split("Kg")[0]);
			setResult(ResultTizhong, intent);
		} else if (BluetoothConstant.XUETANG_TYPENAME.equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE().split("mmol/L")[0]);
			setResult(ResultXuetang, intent);
		} else if (BluetoothConstant.XUEYANG_TYPENAME.equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE().split("%")[0]);
			setResult(ResultXueyang, intent);
		} else if(BluetoothConstant.XUEZHI_TYPENAME.equals(flag)){
			intent.putExtra("value", dataList.get(position).getCL_VALUE());
			setResult(ResultXueZhi, intent);
		}else if (BluetoothConstant.TIWEN_TYPENAME.equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE().split("℃")[0]);
			setResult(ResultTiwen, intent);
		} else if ("血红蛋白".equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE());
			setResult(ResultXueHongDanbai, intent);
		} else if ("脉率".equals(flag) || "心率".equals(flag)) {
			String value_xl = dataList.get(position).getCL_VALUE();
			if (value_xl.contains("次/分")) {
				value_xl = value_xl.split("次/分")[0];
			} else if (value_xl.contains("bpm")) {
				value_xl = value_xl.split("bpm")[0];
			}
			intent.putExtra("value", value_xl);
			setResult(ResultMaiLv, intent);
		} else if (BluetoothConstant.ECG_TYPENAME.equals(flag)) {
			Log.e("eeeeeeeeeeeeeeeee", dataList.get(position).getCL_VALUE());
			intent.putExtra("result", dataList.get(position).getCL_VALUE());
			intent.putExtra("time", dataList.get(position).getCL_TIME());
			setResult(ResultECG, intent);
		} else if (BluetoothConstant.NIAOCHANGGUI_TYPENAME.equals(flag)) {
			intent.putExtra("尿胆原", niaodanyuanList.get(position).getCL_VALUE());
			intent.putExtra("尿潜血", qianxueList.get(position).getCL_VALUE());
			intent.putExtra("胆红素", danhongsuList.get(position).getCL_VALUE());
			intent.putExtra("尿酮体", tongtiList.get(position).getCL_VALUE());
			intent.putExtra("尿糖", putaotangList.get(position).getCL_VALUE());
			intent.putExtra("蛋白质", danbaizhiList.get(position).getCL_VALUE());
			intent.putExtra("PH", pHList.get(position).getCL_VALUE());
			intent.putExtra("亚硝酸盐", yaxiaoList.get(position).getCL_VALUE());
			intent.putExtra("白细胞", baixibaoList.get(position).getCL_VALUE());
			intent.putExtra("比重", bizhongList.get(position).getCL_VALUE());
			intent.putExtra("维生素", weishengsuList.get(position).getCL_VALUE());
			setResult(ResultNiaoChangGui, intent);
		}else if (BluetoothConstant.DGC_TYPENAME.equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE().split("Kg")[0]);
			setResult(ResultDGC, intent);
		}
		finish();
	}

	// 获取对应的心电图片地址
	public static String getXinDianPicture(String time) {
		String pictureFile = "";
		List<SBCL_DATA> list = fdbmanger.findAllByWhere(SBCL_DATA.class,
				"CL_TYPE = '心电图片' AND IDCARD = '" + IDCARD + "' AND CL_TIME='" + time + "'");
		if (list.size() > 0) {
			pictureFile = list.get(0).getCL_VALUE();

		}
		return pictureFile;
	}

}
